<?php

// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\ShopPerson;
use app\crm\model\ShopPersonSign;
use app\crm\model\SignCert;
use think\response\Json;
use think\facade\Db;
use think\facade\Request;
use Yyz\test\src\YiYunzhang\RunTest as YzhRun;


class Sign extends ApiCommon
{

    protected $customerModel;

    protected $customerConfigModel;

    protected $signRun;

    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize(){

        $action = [

            'permission' => ['add_question','shenfenauthtwo','createcerts','addsign','signlist','signdetails','delsign','createwritepadurl','writepadnotify'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth', $action);

        $request = $this->request;


        $a = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }
    }
    //获取护理师签名列表
    public function getSignList(){
//        $shopIds = $this->getCommonShopIdsCollectByUserType();
//        $this->param['shopIds'] = $shopIds;
//        $this->param['user_id'] = $this->userInfo['id'];
//        $this->param['shop_person_id'] = $this->userInfo['shop_person_id'];
        $ShopPersonSignModel = new ShopPersonSign();
        $data = $ShopPersonSignModel->getDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $ShopPersonSignModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }
    //删除签名
    public function personSignDel(){
        try{
            $ShopPersonSignModel = new ShopPersonSign();
            $data = $ShopPersonSignModel->personSignDel($this->param);
            if (!$data) {
                return resultArray(['error' => $ShopPersonSignModel->getError()]);
            }
            return resultArray(['data' => $data,'error'=>'操作成功']);
        }catch (\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    //默认签名
    public function personSignDefault(){
        try{
            $ShopPersonSignModel = new ShopPersonSign();
            $data = $ShopPersonSignModel->personSignDefault($this->param);
            if (!$data) {
                return resultArray(['error' => $ShopPersonSignModel->getError()]);
            }
            return resultArray(['data' => $data,'error'=>'默认成功']);
        }catch (\Exception $e){
            return resultArray(['error' => $e->getMessage()]);
        }
    }






    //创建证书
    public function createCerts(){
        $this->signRun = new YzhRun();
        $data=[];
        $data['idCardNum'] = '411424199803232819';
        $data['real_name'] = '李寅生';
        $data['newMobile'] = '13520351725';
        $url ='/ecs/account/applyCertString.jspa';
        $EsignArr=[
            'info'=>json_encode([
                'type'=>1,
                'cardType'=>'0',
                'idCardNum'=>$data['idCardNum'],
                'name'=>$data['real_name'],
                'mobilePhone'=>$data['newMobile']
            ]),'isSign'=>true,
        ];

        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');

        if($signRes['meta']['code'] != 0 ){
            $returnArr =  ['error'=>$signRes['meta']['message']];
        }else{
            $returnArr =  ['code'=>200,'msg'=>'success','data'=>$signRes['data']];
        }
        return $returnArr;
    }

    //查看证书
    public function certsDetails($data){
        $this->signRun = new YzhRun();
        $url ='/ecs/account/getCertInfo.jspa';
        $EsignArr=[
            'info'=>json_encode([
                'type'=>1,
                'cardType'=>'0',
                'idCardNum'=>$data['idCardNum'],
                'mobilePhone'=>$data['mobilePhone']
            ])
        ];
        $writePadUrl = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
        print_r($writePadUrl);die;
    }


    //个人身份二要素
    public function shenfenAuthTwo(){
        $this->signRun = new YzhRun();
        $url  = '/ecs/face/identityAuthUrl.jspa';
        $EsignArr = [
            'name'   => '李寅生',
            'idNumber' => '411424199803232819',
        ];
        $signCodeRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
        if(isset($signCodeRes['meta']['code']) && $signCodeRes['meta']['code']==0){
            if( $signCodeRes['data']['resultData']['result'] == '一致'){
                $is_uniformity = true;
            }else{
                $is_uniformity = false;
            }
            $returnArr = ['error'=>'success','data'=>['is_uniformity'=>$is_uniformity]];
        }else{
            $returnArr = ['error'=>'error'];
        }
        return resultArray($returnArr);
    }

    //添加个人印章
    public function addSign(){
        $this->signRun = new YzhRun();
        $url  = '/ecs/signInfo/addSign.jspa';
        $EsignArr = [
            'mobilePhone'   => '1143592399323287552',
            'signImg'=> '张三',
            'signType' => '1',
            'signWidth'=>115
        ];
        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');

        if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
            $returnArr = ['error'=>'success','data'=>['sign_id'=>$signRes['data']['signId']]];
        }else{
            $returnArr = ['error'=>'error',];
        }
        return resultArray($returnArr);
    }

    //获取个人印章列表
    public function signList(){
        $this->signRun = new YzhRun();
        $url  = '/ecs/signInfo/getsignList.jspa';
        $EsignArr = [
            'mobilePhone'   => '1143592399323287552',
            'signType' => '1',
        ];
        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
//        echo "<pre>";
//        var_dump($signRes);die;
        if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
            $returnArr = ['error'=>'success','data'=>['list'=>$signRes['data']['list']]];
        }else{
            $returnArr = ['error'=>'error',];
        }
        return resultArray($returnArr);
    }

    //删除印章
    public function delSign(){
        $this->signRun = new YzhRun();
        $url  = '/ecs/signInfo/delSign.jspa';
        $EsignArr = [
            'mobilePhone'   => '1143592399323287552',
            'signId' => '508525',
        ];
        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
        if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
            $returnArr = ['error'=>$signRes['meta']['message'],'data'=>[]];
        }else{
            $returnArr = ['error'=>$signRes['meta']['message']];
        }
        return resultArray($returnArr);
    }

    //签名详情
    public function signDetails(){

        $this->signRun = new YzhRun();
        $url  = '/ecs/signInfo/getSignImg.jspa';
        $EsignArr = [
            'mobilePhone'   => '1143592399323287552',
            'signId' => '508494',
        ];
        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
        if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
            $returnArr = ['error'=>'success','data'=>$signRes['data']];
        }else{
            $returnArr = ['error'=>'error'];
        }
        return resultArray($returnArr);
    }
    //获取手写板
    public function createWritePadUrl(){
        try {
            $param = request()->param();

            $person_id = $param['person_id']??0;
            if(empty($person_id)){
                throw new \Exception('请传入护理师id');
            }
            $person = ShopPerson::find($person_id);
            if(empty($person)){
                throw new \Exception('护理师不存在');
            }
            $userNum = $this->getSignCert($person_id);

            $this->signRun = new YzhRun();
            $url  = '/ecs/signApiPage/handWriting.jspa';
            $EsignArr = [
                'mobilePhone'   => $userNum,
                'returnUrl' => 'https://beautyapi.178590.com/crm/sign/writePadNotify?id='.$person_id,
            ];
            $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');
            if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
                $returnArr = ['error'=>'success','data'=>$signRes['data']];
            }else{
                $returnArr = ['error'=>'error'];
            }
        }catch (\Exception $e){
            $returnArr = ['error'=>$e->getMessage()];
        }
        return resultArray($returnArr);

    }
    //手写板回调
    public function writePadNotify(){

        $param = request()->param();

        $person_id = $param['id'];
        $sign_id = $param['signId'];

        $userNum = $this->getSignCert($person_id);

        $this->signRun = new YzhRun();
        $url  = '/ecs/signInfo/getSignImg.jspa';
        $EsignArr = [
            'mobilePhone'   => $userNum,
            'signId' => $sign_id,
        ];
        $signRes = $this->signRun->applyCertificateTest($EsignArr,$url,'post');

        $personSignModel = new ShopPersonSign();
        $personSignModel->person_id = $person_id;
        $personSignModel->sign_id = $sign_id;
        $personSignModel->create_time = time();
        if(isset($signRes['meta']['code']) && $signRes['meta']['code']==0){
            $personSignModel->sign_img =$signRes['data']['signImg'];
        }
        $personSignModel->save();

        echo '<script>';
        echo 'window.onload = function() {';
        echo '    alert("添加成功！");';
        echo '}';
        echo '</script>';

    }

    public function getSignCert($person_id){
        $userNum = SignCert::where('person_id',$person_id)->value('userNum');
        return $userNum;
    }



}
